package com.shaoshaohuo.app.view;

import java.util.Calendar;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.view.DatepickerPopwindow.OnSelectDateListener;
import com.shaoshaohuo.app.utils.StringHelper;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;

/**
 * Description:
 */
public class DatepickerStartPopwindow implements View.OnClickListener {

	private Context context;
	private PopupWindow hintPopWinwow;

	private Button mCancelButton;
	private Button mOkButton;

	private android.widget.DatePicker mDatePicker;

	private OnSelectDateListener onSelectDateListener;

	public DatepickerStartPopwindow(Context context) {
		this.context = context;
		initView();
	}

	private void initView() {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final View contentView = inflater.inflate(R.layout.popwindow_date, null, false);
		hintPopWinwow = new PopupWindow(contentView, WindowManager.LayoutParams.MATCH_PARENT,
				WindowManager.LayoutParams.WRAP_CONTENT, true);
		hintPopWinwow.setOutsideTouchable(true);
		hintPopWinwow.setBackgroundDrawable(new BitmapDrawable());
		hintPopWinwow.setAnimationStyle(R.style.Anim_Dialog_Bottom);

		mDatePicker = (android.widget.DatePicker) contentView.findViewById(R.id.dp_date);

		mOkButton = (Button) contentView.findViewById(R.id.button_ok);
		mCancelButton = (Button) contentView.findViewById(R.id.button_cancel);

		mOkButton.setOnClickListener(this);
		mCancelButton.setOnClickListener(this);

		Calendar date = Calendar.getInstance();
		date.set(date.get(Calendar.YEAR), date.get(Calendar.MONTH), date.get(Calendar.DAY_OF_MONTH) + 1);
	}

	public void showLocation(View view) {
		hintPopWinwow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
	}

	public void setShowMode(int i) {
		switch (i) {
		case 1:
			((ViewGroup) ((ViewGroup) mDatePicker.getChildAt(0)).getChildAt(0)).getChildAt(1).setVisibility(View.GONE);
			((ViewGroup) ((ViewGroup) mDatePicker.getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE);
			break;
		case 2:
			((ViewGroup) ((ViewGroup) mDatePicker.getChildAt(0)).getChildAt(0)).getChildAt(1).setVisibility(View.GONE);
			break;
		}
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.button_ok:
			if (onSelectDateListener != null) {
				String date = StringHelper.formatDate(
						mDatePicker.getYear() + "-" + (mDatePicker.getMonth() + 1) + "-" + mDatePicker.getDayOfMonth());
				if (!TextUtils.isEmpty(date)) {
					onSelectDateListener.onSelectDate(date);
					hintPopWinwow.dismiss();
				} else {

				}
			}
			break;
		case R.id.button_cancel:
			hintPopWinwow.dismiss();
			break;
		}
	}

	public boolean isShowing() {
		return hintPopWinwow.isShowing();
	}

	public void setOnSelectDateListener(OnSelectDateListener onSelectDateListener) {
		this.onSelectDateListener = onSelectDateListener;
	}


}
